﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluggableModulesInterface;
using Utils;
using StorageManagement;
using SMSROUTER.Business;
using PluggableModulesRouter;
using SMSROUTER.StorageManagement;

namespace StaticThreadManagement
{
    public static class StaticThreadManage
    {
        public static bool MORunning { get; private set; }
        public static bool MTRunning { get; private set; }
        public static WorkingBaseTimer gcCollect = new GCThreadManage();
        public static WorkingBaseTimer XmlReader = new XmlReaderTheadManage();
        public static WorkingBase MTReader = new MTReaderTheadManage();
        public static WorkingBase MTUpdateReport = new MTUpdateReportTheadManage();
        public static WorkingBase MTUpdateReportFRR = new MTUpdateReportFRRTheadManage();
        public static WorkingBase MOReader = new MOReaderTheadManage();
        public static WorkingBase MOUpdateStatistic = new MOUpdateStatisticTheadManage();
        public static WorkingBase threadLog = new LogThreadManage();

        public static void StartAll()
        {
            try
            {
                MTReader.Start();
                MTUpdateReport.Start();
                MTUpdateReportFRR.Start();
                MOReader.Start();
                MOUpdateStatistic.Start();
                threadLog.Start();
                XmlReader.Start();
                gcCollect.Start();
                
                MORunning = true;
                MTRunning = true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void StartMO()
        {
            try
            {
                MOReader.Start();
                MOUpdateStatistic.Start();
                if (!threadLog.IsStart) threadLog.Start();
                if (!XmlReader.IsStart) XmlReader.Start();
                if (!gcCollect.IsStart) gcCollect.Start();

                MORunning = true;
            }
            catch (Exception)
            {                
                throw;
            }
        }

        public static void StartMT()
        {
            try
            {
                MTReader.Start();
                MTUpdateReport.Start();
                MTUpdateReportFRR.Start();
                if (!threadLog.IsStart) threadLog.Start();
                if (!XmlReader.IsStart) XmlReader.Start();
                if (!gcCollect.IsStart) gcCollect.Start();

                MTRunning = true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void StopAll()
        {
            try
            {
                MTReader.Stop();
                MTUpdateReport.Stop();
                MTUpdateReportFRR.Stop();
                MOReader.Stop();
                MOUpdateStatistic.Stop();
                threadLog.Stop();
                XmlReader.Stop();
                gcCollect.Stop();
                
                MORunning = false;
                MTRunning = false;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public static void StopMO()
        {
            try
            {
                MOReader.Stop();
                MOUpdateStatistic.Stop();
                if (!MTReader.IsStart) // Nếu MT cũng stop thì stop nốt
                {
                    threadLog.Stop();
                    XmlReader.Stop();
                    gcCollect.Stop();
                }

                MORunning = false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void StopMT()
        {
            try
            {
                MTReader.Stop();
                MTUpdateReport.Stop();
                MTUpdateReportFRR.Stop();
                if (!MOReader.IsStart) // Nếu MO cũng stop thì stop nốt
                {
                    threadLog.Stop();
                    XmlReader.Stop();
                    gcCollect.Stop();
                }

                MTRunning = false;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public static void Exit()
        {
            try
            {
                //Dừng luồng đọc dữ liệu
                StopAll();
                //Dừng tất cả các plugable module
                PluggableManage.Stop();

                //Đẩy dữ liệu chứa trong queue ngược vào database
                SmsOutWaitingStorage.RestoreData();
                //Cập nhật thông tin SmsOutReport trong các queue vào database
                SmsOutReportStorage.UpdateStatusOnExit();

                //Đẩy dữ liệu chứa trong queue ngược vào database
                SmsInWaitingStorage.RestoreData();
                //Cập nhật thông tin SmsInStatisticStruct trong các queue vào database
                SmsInStatisticStorage.UpdateStatusOnExit();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
